package app.gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;

/**
 *      JDate
 *
 * Rozsireni tridy JFormattedTextField pro pouziti specialne s datem.
 *
 * @author Dejvino
 */
public class JDate extends JFormattedTextField {
    private DateFormat dateFormat;  // formatovac
    
    public JDate(){
        super();
        dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
    }

    /**
     * Nastaveni data do textu prvku
     * @param date Datum k nastaveni
     */
    public void setDate(Date date) {
        setText(dateFormat.format(date));
    }

    /**
     * Ziskani data z textu prvku
     * @return Ziskane datum nebo null, pokud se nepodarilo ziskat
     */
    public Date getDate() {
        Date date = new Date();
        try {
            date = dateFormat.parse(getText());
        } catch (ParseException ex) {
            Logger.getLogger(JDate.class.getName()).log(Level.SEVERE, null, ex);
            date = null;
        }
        return date;
    }
}
